<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" />
            <div class="title">SongGGT OJ</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <a-space>
        <a-button type="primary" @click="startSignIn">
          <icon-edit />
          签到
        </a-button>
      </a-space>
    </a-col>
    <a-col flex="100px">
      <a-space>
        <a-button type="primary" @click="startPost">
          <icon-edit />
          发贴
        </a-button>
      </a-space>
    </a-col>
    <a-col flex="100px">
      <div>
        <a-dropdown>
          <a-avatar
            :style="{
              marginRight: '24px',
              verticalAlign: 'middle',
              backgroundColor: '#14a9f8',
            }"
          >
            <img alt="avatar" :src="avatarUrl" />
          </a-avatar>
          <template #content>
            <a-doption @click="toLogin">立即登录</a-doption>
          </template>
        </a-dropdown>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, onMounted, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import { UserControllerService } from "../../generated";
// import { useUserStore } from "../store/user";

const router = useRouter();
const store = useStore();

// /**
//  * 获取用户是否登录
//  */
// const isLoggedIn = computed<boolean>(() => {
//   return !!store.state.user?.loginUser;
// });

onMounted(() => {
  loadData();
});
//头像
const avatarUrl = ref("");
const loadData = async () => {
  const userId = await UserControllerService.getLoginUserUsingGet();
  const res = await UserControllerService.getUserByIdUsingGet(userId.data?.id);
  if (res.code === 0 && res.data) {
    const userData = res.data;
    avatarUrl.value =
      userData.userAvatar ||
      "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp";
  }
};

// /**
//  * 展示头像
//  */
// let currentUserName = store.state.user.loginUser.userName;
// const text = computed<string>(() => {
//   if (isLoggedIn.value) {
//     return currentUserName.charAt(0).toUpperCase();
//   }
//   return "未登录";
// });

/**
 * 头像下拉框
 */
const toLogin = () => {
  router.push({
    path: "/user/login",
  });
};

// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

// 默认主页
const selectedKeys = ref(["/"]);

// 路由跳转后，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

/**
 * 处理按钮点击事件
 */
const startPost = () => {
  // 跳转到指定页面
  router.push("/add/post");
};
const startSignIn = () => {
  // 跳转到指定页面
  router.push("/SignIn");
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
